package ebcs.scan.controller.sys;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import ebcs.database.bean.sys.PaAgency;
import ebcs.database.service.sys.IAgencyManagerService;
import ebcs.database.service.sys.IUserManagerService;
import ebcs.scan.bean.common.ExcleEntity;
import ebcs.scan.bean.sys.SysUserInfor;
import ebcs.scan.common.utils.ExportDataUtil;
import jplat.core.trans.JAppBaseService;
import jplat.core.trans.JAppContext;
import jplat.core.trans.JAppContextFactory;
import jplat.error.exception.JSystemException;
import jplat.error.exception.JTransException;
import jplat.tools.coder.JsonCoder;


/***
 *  demo管理
 * @author chenzhuo
 * @comment
 */
@RequestMapping("/ns0/demo/")
@Controller
@EnableScheduling
public class DemoManagerCtrl extends JAppBaseService
{
	private Logger logger = Logger.getLogger(DemoManagerCtrl.class);
	protected JAppContextFactory serviceFactory = new JAppContextFactory();

	@Autowired
	private ExportDataUtil excelTools;
	@Autowired
	public IUserManagerService userService ;
	@Autowired
	public IAgencyManagerService agencyService;

	/**
	 * excel下载导出
	 */
	@RequestMapping("/exportExcelFile.do")
	@ResponseBody
	public ResponseEntity<byte[]>  exportExcelFile(HttpServletRequest request, HttpServletResponse response)
			throws JTransException, JSystemException {
		//---对数据bean 的转义
		List headList = new ArrayList();
		Map headMap1 = new HashMap();
		headMap1.put("agenId", "机构ID");
		headMap1.put("name", "名称");
		headList.add(headMap1);
		Map headMap2 = new HashMap();
		headMap2.put("userNo", "用户编号");
		headMap2.put("userName", "用户名称");
		headList.add(headMap2);
		ExcleEntity excleBean = new ExcleEntity();
		excleBean.setFileName("excleFile.xls");
		//---设置excel 的sheet 的title
		excleBean.setTitleArr(new String[] {"机构信息","用户信息"});
		excleBean.setHeadList(headList);
		//----- from db
		PaAgency agency = new PaAgency();
		List agencyList = this.agencyService.searchAgencyInfor(agency);
		List userList = this.userService.searchUserInforByAgenId(1);
		//-----
		List[] reportListArr = new ArrayList[]{(ArrayList) agencyList,(ArrayList) userList};
		byte[] fileByte	= excelTools.exportExcelMoreSheet(excleBean, reportListArr);
		InputStream in=new ByteArrayInputStream(fileByte);//将该文件加入到输入流之中
		byte[] body=null;
		try {
			body=new byte[in.available()];
			in.read(body);
			String fileName = "exportExcelDemo.xls";
			fileName=new String(fileName.getBytes("gbk"),"iso8859-1");//防止中文乱码
			HttpHeaders headers=new HttpHeaders();//设置响应头
			headers.add("Content-Disposition", "attachment;filename="+fileName);
			HttpStatus statusCode = HttpStatus.OK;//设置响应吗
			ResponseEntity<byte[]> res=new ResponseEntity<byte[]>(body, headers, statusCode);
			return res;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}


	/**
	 *
	 */
	@RequestMapping("/test2.do")
	public void test2( HttpServletRequest request,HttpServletResponse response ) throws JTransException, JSystemException{
		JAppContext context = serviceFactory.buildAppContextNoUser(request,response);		//buildAppContextWithUser 自带客户信息.
		Map rsModel = new HashMap();
		rsModel.put("AA", "11");
		rsModel.put("BB", null);
		context.buildAppResponse(rsModel);
	}
	/**
	 *
	 */
	@RequestMapping("/prm/testPermission.do")
	public void testPermission( HttpServletRequest request,HttpServletResponse response ) throws JTransException, JSystemException{
		JAppContext context = serviceFactory.buildAppContextWithUser(request,response);		//buildAppContextWithUser 自带客户信息.
		Map rsModel = new HashMap();
		context.buildAppResponse(null);
	}
	/**
	 *
	 */
	@RequestMapping("getAllLoginUserInfor.do")
	public void getAllLoginUserInfor( HttpServletRequest request,HttpServletResponse response ) throws JTransException, JSystemException{
		JAppContext context = serviceFactory.buildAppContextWithUser(request,response);		//buildAppContextWithUser 自带客户信息.
		List<SysUserInfor> sysList = context.getSession().getAllLoginUserInfor();

		logger.info(JsonCoder.toJsonString(sysList));
		Map rsModel = new HashMap();
		context.buildAppResponse(null);
	}
	public void doTimer() {
// 	 	 "0 0 12 * * ?" 每天中午12点触发 
		//"0 15 10 ? * *" 每天上午10:15触发
		//"0 15 10 * * ?" 每天上午10:15触发
		//"0 15 10 * * ? *" 每天上午10:15触发
		//"0 15 10 * * ? 2005" 2005年的每天上午10:15触发
		//"0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发
		//"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发
		//"0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
		//"0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发
		//"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发
		//"0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发
		//"0 15 10 15 * ?" 每月15日上午10:15触发
		//"0 15 10 L * ?" 每月最后一日的上午10:15触发
		//"0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发
		//"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发
		//"0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发
		//每隔5秒执行一次：*/5 * * * * ?
		//每隔1分钟执行一次：0 */1 * * * ?
		//每天23点执行一次：0 0 23 * * ?
		//每天凌晨1点执行一次：0 0 1 * * ?
		//每月1号凌晨1点执行一次：0 0 1 1 * ?
		//每月最后一天23点执行一次：0 0 23 L * ?
		//每周星期天凌晨1点实行一次：0 0 1 ? * L
		System.out.println("定时任务"+System.currentTimeMillis());
	}
}